package com.atguigu.srb.sms.controller.api;

import com.atguigu.common.exception.Assert;
import com.atguigu.common.result.R;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.common.util.RandomUtils;
import com.atguigu.common.util.RegexValidateUtils;
import com.atguigu.srb.sms.client.CoreUserInfoClient;
import com.atguigu.srb.sms.service.SmsService;
import com.atguigu.srb.sms.service.impl.SmsServiceImpl;
import com.atguigu.srb.sms.util.SmsProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/sms")
@Slf4j
@Api(tags = "短信管理")
public class ApiSmsController {

    @Resource
    private CoreUserInfoClient coreUserInfoClient;
    @Resource
    private RedisTemplate redisTemplate;

    @GetMapping("/send/{mobile}")
    public R sendMessage(@ApiParam(value = "手机号",required = true)
                         @PathVariable String mobile){
        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        Assert.isTrue(RegexValidateUtils.checkCellphone(mobile),ResponseEnum.MOBILE_ERROR);
        boolean res=coreUserInfoClient.mobileCheck(mobile);
        System.out.println("是否存在"+res);
        Assert.isTrue(res==false,ResponseEnum.MOBILE_EXIST_ERROR);
        String code= RandomUtils.getFourBitRandom();
        //将验证码存入redis
        redisTemplate.opsForValue().set("srb:sms:code:"+mobile,code,5, TimeUnit.MINUTES);
        log.info("将验证码存入redis"+" "+"srb:sms:code:"+mobile+"   "+code);
        System.out.println("验证码为:"+code);
        Map<String,Object> param=new HashMap<>();
        param.put("code",code);
        try {
            SmsService smsService=new SmsServiceImpl();
            smsService.send(mobile, SmsProperties.TEMPLATE_CODE, param);
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("程序继续执行");
        return R.ok().message("短信发送成功"+param.toString());
    }

    @GetMapping("/sendRet/{mobile}")
    public R sendRetMessage(@ApiParam(value = "手机号",required = true)
                         @PathVariable String mobile){
        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        Assert.isTrue(RegexValidateUtils.checkCellphone(mobile),ResponseEnum.MOBILE_ERROR);
        String code= RandomUtils.getFourBitRandom();
        //将验证码存入redis
        redisTemplate.opsForValue().set("srb:sms:code:"+mobile,code,5, TimeUnit.MINUTES);
        log.info("将验证码存入redis"+" "+"srb:sms:code:"+mobile+"   "+code);
        System.out.println("验证码为:"+code);
        Map<String,Object> param=new HashMap<>();
        param.put("code",code);
        try {
            SmsService smsService=new SmsServiceImpl();
            smsService.send(mobile, SmsProperties.TEMPLATE_CODE, param);
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("程序继续执行");
        return R.ok().message("短信发送成功"+param.toString());
    }

    @GetMapping("/check/{code}/{mobile}")
    public R checkCode(@ApiParam(value = "验证码",required = true)
                       @PathVariable String code,
                       @ApiParam(value = "手机号",required = true)
                       @PathVariable String mobile){
        //将验证码存入redis
        String checkCode = (String)redisTemplate.opsForValue().get("srb:sms:code:" + mobile);
        log.info("验证码为"+checkCode);
        if (checkCode.equals(code)){
            return R.ok().message("验证码正确");
        }else {
            return R.error().message("验证码错误");
        }
    }
}
